//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_board_dao

import (
	"context"
	"fmt"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_board_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type EdgeDbItem struct {
	EdgeId       string `bson:"_id"` //自动生成
	BoardId      string `bson:"boardId"`
	ProjectId    string `bson:"projectId"`
	FromNodeId   string `bson:"fromNodeId"`
	FromHandleId string `bson:"fromHandleId"`
	ToNodeId     string `bson:"toNodeId"`
	ToHandleId   string `bson:"toHandleId"`
	Label        string `bson:"label"`
}

func (item *EdgeDbItem) ToEdge() *project_board_api.Edge {
	return &project_board_api.Edge{
		EdgeKey: &project_board_api.EdgeKey{
			FromNodeId:   item.FromNodeId,
			FromHandleId: item.FromHandleId,
			ToNodeId:     item.ToNodeId,
			ToHandleId:   item.ToHandleId,
		},
		Label: item.Label,
	}
}

type _EdgeDao struct {
	mc *mongo.Collection
}

func newEdgeDao(db *mongo.Database) (*_EdgeDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT_BOARD + "edge")
	_, err := mc.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
		{
			Keys: bson.D{{"boardId", 1}, {"fromNodeId", 1}},
		},
		{
			Keys: bson.D{{"boardId", 1}, {"toNodeId", 1}},
		},
	})
	if err != nil {
		return nil, err
	}
	return &_EdgeDao{mc: mc}, nil
}

func (db *_EdgeDao) Insert(ctx context.Context, dbItem *EdgeDbItem) error {
	dbItem.EdgeId = db.genEdgeId(dbItem.BoardId, dbItem.FromNodeId, dbItem.FromHandleId, dbItem.ToNodeId, dbItem.ToHandleId)
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_EdgeDao) genEdgeId(boardId, fromNodeId, fromHandleId, toNodeId, toHandleId string) string {
	return fmt.Sprintf("%s%s%s%s%s", boardId, fromNodeId, fromHandleId, toNodeId, toHandleId)
}

func (db *_EdgeDao) Exist(ctx context.Context, boardId, fromNodeId, fromHandleId, toNodeId, toHandleId string) (bool, error) {
	edgeId := db.genEdgeId(boardId, fromNodeId, fromHandleId, toNodeId, toHandleId)
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": edgeId, "boardId": boardId})
	return count > 0, err
}

func (db *_EdgeDao) Get(ctx context.Context, boardId, fromNodeId, fromHandleId, toNodeId, toHandleId string) (*EdgeDbItem, error) {
	edgeId := db.genEdgeId(boardId, fromNodeId, fromHandleId, toNodeId, toHandleId)
	retItem := &EdgeDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": edgeId, "boardId": boardId}).Decode(retItem)
	return retItem, err
}

func (db *_EdgeDao) Remove(ctx context.Context, boardId, fromNodeId, fromHandleId, toNodeId, toHandleId string) error {
	edgeId := db.genEdgeId(boardId, fromNodeId, fromHandleId, toNodeId, toHandleId)
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": edgeId, "boardId": boardId})
	return err
}

func (db *_EdgeDao) RemoveByBoard(ctx context.Context, boardId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"boardId": boardId})
	return err
}

func (db *_EdgeDao) RemoveByFromNode(ctx context.Context, boardId, fromNodeId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"boardId": boardId, "fromNodeId": fromNodeId})
	return err
}

func (db *_EdgeDao) RemoveByToNode(ctx context.Context, boardId, toNodeId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"boardId": boardId, "toNodeId": toNodeId})
	return err
}

func (db *_EdgeDao) List(ctx context.Context, projectId, boardId string) ([]*EdgeDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": projectId, "boardId": boardId})
	if err != nil {
		return nil, err
	}
	retList := []*EdgeDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_EdgeDao) Update(ctx context.Context, boardId, fromNodeId, fromHandleId, toNodeId, toHandleId string, label string) error {
	edgeId := db.genEdgeId(boardId, fromNodeId, fromHandleId, toNodeId, toHandleId)
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": edgeId, "boardId": boardId}, bson.M{"$set": bson.M{"label": label}})
	return err
}
